const express = require('express');
const router = express.Router();
const mysql = require('mysql');
const bodyParser = require('body-parser');

const urlencodeParser = bodyParser.urlencoded({
    extended: true,
    limit: '50mb'
});
const connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '12345678',
    database: 'internetcafe'
});
connection.connect(function (err) {
    if (err) {
        console.error('连接失败: ' + err.stack);
        return;
    }
    console.log('连接成功 id ' + connection.threadId);
})
router.get("/selectcomputer", function (req, res) {
    connection.query('select * from computer', function (err, result) {
        if (err) {
            console.log(err);
            res.jsonp({
                fail: 1
            })
        } else {
            res.jsonp({
                result: result,
            })
        }
    })
});
router.get('/addcomputer', function (req, res) {
    let data = req.query;
    console.log(data)
    let tempdata = {
        id: null,
        no: data.no,
        price: data.price,
        img:'http://miao.su/images/2019/10/03/464c5f7a4c195bcc4d6b9.jpg'

    }
    //检验是否为空
    if (tempdata.no == '' || tempdata.price == '') {
        res.jsonp({
            code: 0,
            msg: '你输入信息不能为空。'
        })
        return;
    }
            connection.query('insert into computer set ?', tempdata, (err, result) => {
                if (err) {
                    console.log(err);
                    res.jsonp({
                        fail: 1
                    })
                } else {
                    res.jsonp({
                        code: 1,
                        result: data
                    })
                }
            })

})
router.get('/updatecomputer', function (req, res) {
    let data = req.query
    console.log(req.query)
    // console.log(window.location.search)
    // return;
    let tempdata = {
        price: data.price,
    }
    //检验是否为空
    if (tempdata.price == '') {
        res.jsonp({
            code: 0,
            msg: '你输入信息不能为空。'
        })
        return;
    } else {
        connection.query('update computer set ? ', tempdata, function (err, result) {
            if (err) {
                console.log
                res.jsonp({
                    fail: 1
                });
            } else {
                res.jsonp({
                    code: 1,
                })
            }
        });
    }
});
router.get("/delcomputer", function (req, res) {
    let data = req.query
    console.log(data.id)
    connection.query('delete from  computer where id=  "' + data.id + '"  ', function (err, result) {
        if (err) {
            console.log(err);
            res.jsonp({
                fail: 1
            })
        } else {
            res.jsonp({
                result: result,
                code: 1,
                msg: '删除成功。'
            })
        }
    })
});
//查询电脑数量
router.get("/selectcomputercount", function (req, res) {
    connection.query('select count(*) count from computer', function (err, rows, result) {
        if (err) {
            console.log(err);
            res.jsonp({
                fail: 1
            })
        } else {
            res.jsonp({
                result: rows[0].count,
            })
        }
    })
});
module.exports = router;